<template>
  <div>
    <!-- 容器 -->
    <el-container class="home-container">
      <!-- header -->
      <el-header>
        <div class="home_header">
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logOut">退出</el-button>
      </el-header>
      <el-container>
        <!-- aside左侧栏 text-color文本颜色 active处于活跃状态的文本颜色 unique-opened只保持一个子菜单的展开-->
        <!-- collapse 是否开启水平折叠  collapse-transition折叠动画-->
        <!-- router 路由重定向 index属性值作为路由跳转-->
        <el-aside :width="isCollapse?'64px':'200px'">
          <!-- 折叠与展开左侧侧边栏 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
            router>
            <!-- 一级菜单 用户管理 index="唯一的标志"点击item.id就展开item -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+item.path" v-for="item in item.children" :key="item.id" @click="saveNavState('/'+item.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{item.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- main右侧栏 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default ({
  name: 'home',
  data () {
    return {
      // 是否折叠
      isCollapse: false,
      // 活跃状态的path
      activePath: '',
      menuList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  // dom元素创建成功--还没挂载到Dom中
  created () {
    // 调用获取数据的函数得到了数据--渲染到menu中
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logOut () {
      // 1点击退出删除token值
      window.sessionStorage.clear()
      // 2页面跳转到登录页面
      this.$router.push('/login')
    },
    async getMenuList () {
      // 返回的是promise对象--解决回调地狱的问题
      const { data: res } = await this.$http.get('/menus')
      // 获取数据失败提示错误
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      // 获取数据成功
      this.menuList = res.data
    },
    // 点击是否折叠
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 点击二级菜单把路径保存到sessionStorage中
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
})
</script>

<style lang="less" scoped>
.home-container{
  height: 100vh;
}
.el-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #373d41;
  padding-left: 0;
  .home_header{
    display: flex;
    align-items: center;
  }
}
.el-aside{
  background: #333744;
  .toggle-button{
    color: #fff;
    background-color: #4a5064;
    font-size: 10px;
    text-align: center;
    line-height: 24px;
    // 字符间距
    letter-spacing: 0.2em;
  }
  .el-menu{
    border-right: 0;
    .el-submenu{
      span{
        margin-left: 10px;
        }
      }
    }
  }
.el-main{
  background: #eaedf1;
}
</style>
